package com.zhongxin.wymovieticket;

import org.json.JSONArray;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.zhongxin.wymovieapi.Client;
import com.zhongxin.wymovieticket.adapter.HotMovieAdapter;
import com.zhongxin.wymovieticket.getdata.DataStore;
import com.zhongxin.wymovieticketentity.HotContent;
import com.zhongxin.wymovieticketentity.HotMovies;
import com.zhongxin.wymovieticketentity.MovieDetail;
import com.zhoujing.utils.ImageDownLoad;
import com.zhoujing.utils.ImageDownLoadUtils;
import com.zhoujing.utils.JsonUtils;
import com.zhoujing.utils.JsonUtils.JsonObjectCallBack;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class HotMovieDetailActivity extends Activity implements OnClickListener {

	private HotMovies hotMovies;
	private MovieDetail movieDetail;
	private ImageView playImageView;
	private TextView hotmovie_detail_pagetilte, hotmovie_detail_highlight;
	private ImageView hotmovie_detail_juzhao;
	private RatingBar hotmovie_detail_gradebar;
	private RequestQueue queue;
	private TextView hotmovie_detail_grade, hotmovie_detail_onlinetime,
			hotmovie_detail_director, hotmovie_detail_actors,
			hotmovie_detail_area, hotmovie_detail_category,
			hotmovie_detail_timeList, hotmovie_detail_content;
	int flag = 1;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		queue = Volley.newRequestQueue(this);
		setContentView(R.layout.activity_hot_movie_detail);
		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra("detail");
		hotMovies = (HotMovies) bundle.getSerializable("hotmovie");
		hotmovie_detail_pagetilte = (TextView) findViewById(R.id.hotmovie_detail_pagetilte);
		playImageView = (ImageView) findViewById(R.id.activity_hotmovie_detail_playimage);
		hotmovie_detail_highlight = (TextView) findViewById(R.id.hotmovie_detail_highlight);
		hotmovie_detail_juzhao = (ImageView) findViewById(R.id.hotmovie_detail_juzhaopic);
		hotmovie_detail_gradebar = (RatingBar) findViewById(R.id.hotmovie_detail_gradebar);
		hotmovie_detail_grade = (TextView) findViewById(R.id.hotmovie_detail_grade);
		hotmovie_detail_onlinetime = (TextView) findViewById(R.id.hotmovie_detail_onlinetime);
		hotmovie_detail_director = (TextView) findViewById(R.id.hotmovie_detail_director);
		hotmovie_detail_actors = (TextView) findViewById(R.id.hotmovie_detail_actors);
		hotmovie_detail_area = (TextView) findViewById(R.id.hotmovie_detail_area);
		hotmovie_detail_category = (TextView) findViewById(R.id.hotmovie_detail_category);
		hotmovie_detail_timeList = (TextView) findViewById(R.id.hotmovie_detail_timeList);
		hotmovie_detail_content = (TextView) findViewById(R.id.hotmovie_detail_content);
		getData();
		hotmovie_detail_pagetilte.setText(hotMovies.getName());

		hotmovie_detail_highlight.setText("“" + hotMovies.getHighlight() + "“");
		// TODO hotmovie_detail_juzhao剧照
		// TODO ratingbar
		hotmovie_detail_grade.setText("" + hotMovies.getGrade() + "分");
		hotmovie_detail_gradebar.setRating((int) hotMovies.getGrade()*5/10);
		hotmovie_detail_onlinetime
				.setText("上映时间：" + hotMovies.getReleaseDate());
		hotmovie_detail_director.setText("导演：" + hotMovies.getDirector());
		hotmovie_detail_actors.setText("主演：" + hotMovies.getActors());
		hotmovie_detail_area.setText(hotMovies.getArea());
		hotmovie_detail_category.setText(hotMovies.getCategory());
		hotmovie_detail_timeList.setText(hotMovies.getDuration());
		
		
		playImageView.setOnClickListener(this);

	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.activity_hotmovie_detail_playimage:
			Intent it = new Intent();
			it.setAction(Intent.ACTION_VIEW);
			it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			// 该地址随着传入的mp4的地址而改变
			Uri uri = Uri.parse(hotMovies.getMobilePreview());// 此url就是流媒体文件的下载地址
			it.setDataAndType(uri, "video/*");// type的值是 "video/*"或者
												// "audio/*"
			startActivity(it);
			
			break;

		default:
			break;
		}
		
	}

	public void getData() {
		String url = "http://piao.163.com/m/movie/detail.html?account_id=&movie_id="
				+ hotMovies.getId()
				+ "&city=440300&apiVer=14&mobileType=android&deviceId=08:00:27:f4:e2:00&channel=netease&ver=4.8";
		JsonUtils.parseJsonObject(this, url, new JsonObjectCallBack() {

			public void jsonObjectCallBack(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.i("---------------------",
						"---------------" + jsonObject.toString());
				try {
					int rtecode = jsonObject.getInt("retcode");
					Log.i("---------------------", "---------------" + rtecode);
					String retdesc = jsonObject.getString("retdesc");
					JSONObject object = jsonObject.getJSONObject("object");
					movieDetail = new MovieDetail();
					movieDetail.parseJson(object);
					hotmovie_detail_content.setText(movieDetail
							.getDescription());

					hotmovie_detail_content
							.setOnClickListener(new View.OnClickListener() {

								public void onClick(View v) {
									// TODO Auto-generated method stub

									if (flag == 1) {
										hotmovie_detail_content.setLines(3);
										flag = 0;
									} else {
										hotmovie_detail_content.setLines(10);
										flag = 1;
									}

								}
							});
					// TODO 下载电影的logo
					ImageDownLoadUtils.downLoadImage(movieDetail.getLogo3(),
							queue, HotMovieDetailActivity.this, playImageView);

					ImageDownLoadUtils.downLoadImage(movieDetail.getLogo(),
							queue, HotMovieDetailActivity.this,
							hotmovie_detail_juzhao);

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}, queue);

	}

	
}
